// 战斗核心类 - 负责协调整个战斗系统
export class BattleCore {
    private static _instance: BattleCore;
    
    // 管理器引用
    private entityManager: EntityManager;
    private eventSystem: EventSystem;
    private timeManager: TimeManager;
    private configManager: ConfigManager;
    
    // 控制器引用
    private battleController: BattleController;
    private inputController: InputController;
    
    // 模型引用
    private battleModel: BattleModel;
    private playerModel: PlayerModel;
    
    // 视图引用
    private battleView: BattleView;
    
    // 战斗状态
    private battleState: BattleState = BattleState.INIT;
    
    public static get instance(): BattleCore {
        if (!this._instance) {
            this._instance = new BattleCore();
        }
        return this._instance;
    }
    
    // 初始化战斗系统
    public initialize(config: BattleConfig): void {
        // 初始化各个管理器
        this.eventSystem = new EventSystem();
        this.entityManager = new EntityManager();
        this.timeManager = new TimeManager();
        this.configManager = new ConfigManager();
        this.configManager.loadConfig(config);
        
        // 初始化模型
        this.battleModel = new BattleModel(config);
        this.playerModel = new PlayerModel();
        
        // 初始化控制器
        this.battleController = new BattleController(this.battleModel);
        this.inputController = new InputController();
        
        // 初始化视图
        this.battleView = new BattleView();
        
        // 注册事件监听
        this.registerEvents();
    }
    
    // 开始战斗
    public startBattle(): void {
        this.battleState = BattleState.RUNNING;
        this.battleController.startBattle();
        this.battleView.showStartAnimation(() => {
            this.eventSystem.emit(BattleEvent.BATTLE_STARTED);
        });
    }
    
    // 暂停战斗
    public pauseBattle(): void {
        if (this.battleState === BattleState.RUNNING) {
            this.battleState = BattleState.PAUSED;
            this.timeManager.pause();
            this.eventSystem.emit(BattleEvent.BATTLE_PAUSED);
        }
    }
    
    // 继续战斗
    public resumeBattle(): void {
        if (this.battleState === BattleState.PAUSED) {
            this.battleState = BattleState.RUNNING;
            this.timeManager.resume();
            this.eventSystem.emit(BattleEvent.BATTLE_RESUMED);
        }
    }
    
    // 结束战斗
    public endBattle(isWin: boolean): void {
        this.battleState = BattleState.ENDED;
        this.timeManager.pause();
        this.battleController.endBattle(isWin);
        this.eventSystem.emit(BattleEvent.BATTLE_ENDED, isWin);
    }
    
    // 更新
    public update(dt: number): void {
        if (this.battleState !== BattleState.RUNNING) return;
        
        // 更新时间管理器
        const scaledDt = this.timeManager.getScaledDeltaTime(dt);
        
        // 更新控制器
        this.inputController.update(scaledDt);
        this.battleController.update(scaledDt);
        
        // 更新实体管理器
        this.entityManager.update(scaledDt);
    }
    
    // 注册事件监听
    private registerEvents(): void {
        // 监听玩家输入事件
        this.eventSystem.on(InputEvent.JOYSTICK_MOVE, this.onJoystickMove, this);
        this.eventSystem.on(InputEvent.SKILL_BUTTON_PRESSED, this.onSkillButtonPressed, this);
        
        // 监听战斗事件
        this.eventSystem.on(BattleEvent.WAVE_COMPLETED, this.onWaveCompleted, this);
        this.eventSystem.on(BattleEvent.PLAYER_DIED, this.onPlayerDied, this);
        this.eventSystem.on(BattleEvent.BOSS_DIED, this.onBossDied, this);
    }
    
    // 事件处理方法
    private onJoystickMove(direction: cc.Vec2, power: number): void {
        const hero = this.entityManager.getHero();
        if (hero) {
            hero.controller.move(direction, power);
        }
    }
    
    private onSkillButtonPressed(skillId: number): void {
        const hero = this.entityManager.getHero();
        if (hero) {
            hero.controller.useSkill(skillId);
        }
    }
    
    private onWaveCompleted(waveIndex: number): void {
        this.battleController.prepareNextWave();
    }
    
    private onPlayerDied(): void {
        // 检查是否可以复活
        if (this.playerModel.canRevive()) {
            this.battleView.showReviveUI(() => {
                this.playerModel.useRevive();
                const hero = this.entityManager.getHero();
                hero.revive();
            });
        } else {
            this.endBattle(false);
        }
    }
    
    private onBossDied(bossId: number): void {
        // 检查是否是最终Boss
        if (this.battleModel.isLastBoss(bossId)) {
            this.endBattle(true);
        }
    }
    
    // 获取器
    public getEntityManager(): EntityManager {
        return this.entityManager;
    }
    
    public getEventSystem(): EventSystem {
        return this.eventSystem;
    }
    
    public getBattleModel(): BattleModel {
        return this.battleModel;
    }
    
    public getBattleState(): BattleState {
        return this.battleState;
    }
}

// 战斗状态枚举
export enum BattleState {
    INIT,
    RUNNING,
    PAUSED,
    ENDED
}

// 战斗事件枚举
export enum BattleEvent {
    BATTLE_STARTED,
    BATTLE_PAUSED,
    BATTLE_RESUMED,
    BATTLE_ENDED,
    WAVE_STARTED,
    WAVE_COMPLETED,
    PLAYER_DIED,
    BOSS_DIED
}

// 输入事件枚举
export enum InputEvent {
    JOYSTICK_MOVE,
    SKILL_BUTTON_PRESSED
}